﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModuleDALCore;
using System.Data;
using DTO;

namespace DAL
{
    public class BENHNHANDAO:DALBase
    {
        public BENHNHANDAO()
        {
            this.OpenConnect();
        }
        private DataTable dt;
        private string _SpName;

        public string SpName
        {
            get { return "SPBENHNHANW"; }
            set { _SpName = value; }
        }
        //get all data
        public List<BENHNHAN> GetListBN(BENHNHAN bn)
        {
            List<BENHNHAN> benhnhanc = new List<BENHNHAN>();
            dt = new DataTable();
			try
			{

				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 0));
				benhnhanc = (List<BENHNHAN>)ConvertDatatableToList.ConvertTo<BENHNHAN>(dt);
			}
			catch { return null; }
            return benhnhanc;
        }
		public DataTable GETALL()
		{
			dt = new DataTable();
			BENHNHAN bnnew = new BENHNHAN();
			foreach (USERS u in Session.USER)
			{
				bnnew.IDCHINHANH = u.IDCONGTY;
			}
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bnnew, null), ConvertClassToArray.GetValues(bnnew, 0));
			}
			catch { return null; }
			return dt;
		}
		public DataTable GETALLSMS()
		{
			dt = new DataTable();
			BENHNHAN bnnew = new BENHNHAN();
			foreach (USERS u in Session.USER)
			{
				bnnew.IDCHINHANH = u.IDCONGTY;
			}
			try
			{

				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bnnew, null), ConvertClassToArray.GetValues(bnnew, 13));

			}
			catch { return null; }
			return dt;
		}
		public List<BENHNHAN> GetListBNDIEUTRI()
		{
			BENHNHAN bnnew = new BENHNHAN();
			foreach (USERS u in Session.USER)
			{
				bnnew.IDCHINHANH = u.IDCONGTY;
			}
			List<BENHNHAN> benhnhanc = new List<BENHNHAN>();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bnnew, null), ConvertClassToArray.GetValues(bnnew, 8));
				benhnhanc = (List<BENHNHAN>)ConvertDatatableToList.ConvertTo<BENHNHAN>(dt);
			}
			catch { return null; }
			return benhnhanc;
		}

		public List<BENHNHAN> GetListTHANHTOAN()
		{
			BENHNHAN bnnew = new BENHNHAN();
			foreach (USERS u in Session.USER)
			{
				bnnew.IDCHINHANH = u.IDCONGTY;
			}
			List<BENHNHAN> benhnhanc = new List<BENHNHAN>();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bnnew, null), ConvertClassToArray.GetValues(bnnew, 11));
				benhnhanc = (List<BENHNHAN>)ConvertDatatableToList.ConvertTo<BENHNHAN>(dt);
			}
			catch { return null; }
			return benhnhanc;
		}
		public List<CT_TOATHUOCBN> GetCTTTbyIDBN(BENHNHAN bn)
		{
			foreach (USERS u in Session.USER)
			{
				bn.IDCHINHANH = u.IDCONGTY;
			}
			List<CT_TOATHUOCBN> cttt = new List<CT_TOATHUOCBN>();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 9));
				cttt = (List<CT_TOATHUOCBN>)ConvertDatatableToList.ConvertTo<CT_TOATHUOCBN>(dt);
			}
			catch { return null; }
			return cttt;
		}
		//find 
		public List<BENHNHAN> Find(BENHNHAN bn)
		{
			foreach (USERS u in Session.USER)
			{
				bn.IDCHINHANH = u.IDCONGTY;
			}
			List<BENHNHAN> benhnhanc = new List<BENHNHAN>();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 1));
				benhnhanc = (List<BENHNHAN>)ConvertDatatableToList.ConvertTo<BENHNHAN>(dt);
			}
			catch { return null; }
			return benhnhanc;
		}
		public List<BENHNHAN> Findthanhtoan(BENHNHAN bn)
		{
			foreach (USERS u in Session.USER)
			{
				bn.IDCHINHANH = u.IDCONGTY;
			}
			List<BENHNHAN> benhnhanc = new List<BENHNHAN>();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 12));
				benhnhanc = (List<BENHNHAN>)ConvertDatatableToList.ConvertTo<BENHNHAN>(dt);
			}
			catch { return null; }
			return benhnhanc;
		}
		public BENHNHAN GetABN(BENHNHAN bn)
		{
			foreach (USERS u in Session.USER)
			{
				bn.IDCHINHANH = u.IDCONGTY;
			}
			BENHNHAN benhnhanc = new BENHNHAN();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 10));
				benhnhanc = (BENHNHAN)ConvertDatatableToList.ConvertToObject<BENHNHAN>(dt);
			}
			catch { return null; }
			return benhnhanc;
		}
		public DataTable Finds(BENHNHAN bn)
		{
			foreach (USERS u in Session.USER)
			{
				bn.IDCHINHANH = u.IDCONGTY;
			}
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 1));
			}
			catch { return null; }
			return dt;
		}
		public String GetSymbol()
		{
			CONGTY ty = new CONGTY();
			foreach (USERS u in Session.USER)
			{
				ty.IDCONGTY = u.IDCONGTY;
			}
			CONGTY ctynhanve = new CONGTY();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable("SPCONGTY", ConvertClassToArray.GetParamName(ty, null), ConvertClassToArray.GetValues(ty, 5));
				if(dt.Rows.Count>0)
					ctynhanve = (CONGTY)ConvertDatatableToList.ConvertToObject<CONGTY>(dt);
			}
			catch { return null; }
			return ctynhanve.KYHIEUBN;

		}
		/// <summary>
		/// SU LY LAI HAM NAY
		/// </summary>
		/// <returns></returns>
		public String CreatenewId()
        {
            try
            {
				BENHNHAN bn = new BENHNHAN();
				foreach (USERS u in Session.USER)
				{
					bn.IDCHINHANH = u.IDCONGTY;
				}

				return Code( DBManager.Intance.ExecuteNonQuery(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 2)).ToString());

            }
			catch { return null; }
        }
		public string format(string num)
		{ 
			string chuoi="";
			int leng = countsymbol(num);
			for (int i = 0; i < 5-leng; i++)
			{
				chuoi += "0";
			}
			return chuoi+num;

		}
		public string Code(String s)
		{
			return "K" + GetSymbol() + format(s);
		}
		public int countsymbol(String s)
		{
			return s.Length;
		}
		//insert 
		public void BEGINTRANSACTION()
		{
			DBManager.Intance.BeginTransaction();
		}
		public void CLOSE()
		{
			DBManager.Intance.CloseConnection();
		}
		public void COMMIT()
		{
			DBManager.Intance.CommitTransaction();
		}
			public void ROLLBACK()
		{
			DBManager.Intance.RollbackTransaction();
		}
		public DataTable GETLISTBIRTHDAY()
		{
			dt = new DataTable();
			BENHNHAN bn = new BENHNHAN();
			foreach (USERS u in Session.USER)
			{
				bn.IDCHINHANH = u.IDCONGTY;
			}
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 7));
				return dt;
			}
			catch { return null; }
		}
		#region TIEN SU BENH

				public bool UPDATETS(TIENSUBENHNHAN cv)
				{

					try
					{
						return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPTIENSUBENH", ConvertClassToArray.GetParamName(cv, null), ConvertClassToArray.GetValues(cv, 2)) == 1);

					}
					catch { return false;}
				}
				public bool INSERTTS(TIENSUBENHNHAN cv)
				{

					try
					{
						return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPTIENSUBENH", ConvertClassToArray.GetParamName(cv, null), ConvertClassToArray.GetValues(cv, 1)) == 1);

					}
					catch { return false;}
				}
		#endregion
		#region  LYDO
		public bool INSERTLDO(LYDOBIETNHAKHOA cv)
		{

			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPLYDOBIETNHAKHOA", ConvertClassToArray.GetParamName(cv, null), ConvertClassToArray.GetValues(cv, 2)) == 1);

			}
			catch { return false; }
		}
		public bool UPDATELDO(LYDOBIETNHAKHOA cv)
		{

			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPLYDOBIETNHAKHOA", ConvertClassToArray.GetParamName(cv, null), ConvertClassToArray.GetValues(cv, 3)) == 1);

			}
			catch { return false;}
		}
		#endregion
		public bool INSERT(BENHNHAN bn, List<TIENSUBENHNHAN> tiensu, LYDOBIETNHAKHOA lydo)
		{
			BEGINTRANSACTION();
			bool kq = true;
			try
			{
				//insert nhan vien 
				foreach (USERS u in Session.USER)
				{
					bn.IDCHINHANH = u.IDCONGTY;
				}
				long nv = INSERTBN(bn);
				if (nv > 0)
				{
					//insert ly do
					lydo.IDBENHNHAN = nv;
					if (!INSERTLDO(lydo))
					{
						kq = false;
					}
					else
					{
						//insert tien su 
						foreach (TIENSUBENHNHAN ts in tiensu)
						{
							TIENSUBENHNHAN ts1 = new TIENSUBENHNHAN();
							ts1 = ts;
							ts1.IDBENHNHAN = nv;
							if (!INSERTTS(ts1))
							{
								kq = false;
								break;
							}
						}
					}
				}
				else
				{
					kq = false;
				}
				if (kq)
				{
					this.COMMIT();
				}
				else
				{
					this.ROLLBACK();
				}
			}
			catch 
			{
				this.ROLLBACK();
				return false;
			}
			finally
			{
				this.CLOSE();
			}
			return kq;
		}
		public bool UPDATE(BENHNHAN bn, List<TIENSUBENHNHAN> tiensu, LYDOBIETNHAKHOA lydo)
		{
			BEGINTRANSACTION();
			bool kq = true;
			try
			{
				foreach (USERS u in Session.USER)
				{
					bn.IDCHINHANH = u.IDCONGTY;
				}
				if (!UPDATEBN(bn))
				{
					kq = false;
					
				}
				lydo.IDBENHNHAN = bn.IDBENHNHAN;
				if (!UPDATELDO(lydo))
					kq = false;
				//delete all lydo cua benh nhan do 
				TIENSUBENHNHAN tsd = new TIENSUBENHNHAN();
				tsd.IDBENHNHAN = bn.IDBENHNHAN;
				if (DELETETIENSU(tsd))
				{
					foreach (TIENSUBENHNHAN ts in tiensu)
					{
						TIENSUBENHNHAN ts1 = new TIENSUBENHNHAN();
						ts1 = ts;
						ts1.IDBENHNHAN = bn.IDBENHNHAN;
						if (!INSERTTS(ts1))
						{
							kq = false;
							break;
						}
					}
				}
				else
				{
					kq = false;
				}
				if (kq)
					COMMIT();
				else
					ROLLBACK();

			}
			catch
			{
				ROLLBACK();
			}
			finally
			{
				this.CLOSE();
			}
			return kq;
		}
		#region BENH NHAN
		public bool DELETETIENSU(TIENSUBENHNHAN bn)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPTIENSUBENH", ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 5)) == 1);
			}
			catch { return false; }
   		}
		public long INSERTBN(BENHNHAN bn)
		{
			try
			{
				return long.Parse(DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 3)).ToString());
			}
			catch { return 0;}
		}
		public bool UPDATEBN(BENHNHAN bn)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 5))==1);
			}
			catch { return false;}
		}
		#endregion
		public BENHNHAN GetABenhNhan(BENHNHAN bn)
		{
			BENHNHAN benhnhanc = new BENHNHAN();
			foreach (USERS u in Session.USER)
			{
				bn.IDCHINHANH = u.IDCONGTY;
			}
			dt = new DataTable();
			try
			{

				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 4));
				benhnhanc = (BENHNHAN)ConvertDatatableToList.ConvertToObject<BENHNHAN>(dt);
			}
			catch { return null;}
			return benhnhanc;
		}
		public bool DELETE(BENHNHAN bn)
		{
			foreach (USERS u in Session.USER)
			{
				bn.IDCHINHANH = u.IDCONGTY;
			}
			try
			{
				return (DBManager.Intance.ExecuteNonQuery(SpName, ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 6)) == 1);
			}
			catch { return false;}
		}
		
    }
}
